package com.miaoa.jbox2d.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ImageView;

import com.miaoa.jbox2d.demo.databinding.LayoutJboxBinding;

public class MainActivity extends AppCompatActivity {

    private JBox2DView mJBoxView;
    private ImageView mIvPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mJBoxView = findViewById(R.id.jbox_view);
        mIvPreview = findViewById(R.id.iv_preview);
        mJBoxView.setImageResource(R.mipmap.ic_save_money_plan_default);
        mJBoxView.setPrefix("￥");
        mJBoxView.postDelayed(() -> {
            mJBoxView.setMoneyNoAnimator(11.9999);
        }, 1000L);
        mJBoxView.setPlanMoney(60000);
    }

    public void addBody(View view) {
        mJBoxView.addMoney(11111);
    }

    public void deleteBody(View view) {
        mJBoxView.removeMoney(110.0);
    }

    public void createBitmap(View view) {
        LayoutJboxBinding binding = LayoutJboxBinding.inflate(getLayoutInflater());
        binding.jboxView.initDrawable()
                .setPrefix("￥")
                .setImageResource(R.mipmap.ic_save_money_plan_default)
                .createWorld(JBox2DImpl.dp2px(this, 300f))
                .setPlanMoney(60000)
                .setMoneyNoAnimator(11.9999);
        Bitmap bitmap = BitmapUtil.createViewBitmap(binding.getRoot());
        mIvPreview.setImageBitmap(bitmap);
        binding.jboxView.release();
        binding.jboxView.destroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mJBoxView.resume();
    }

    @Override
    protected void onStop() {
        mJBoxView.stop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mJBoxView.destroy();
        super.onDestroy();
    }
}